<?php

class ManufacturerTable extends Doctrine_Table {

    const ACTIVE = 1;
    const PASSIVE = 2;

    public static function getInstance() {
        return Doctrine_Core::getTable('Manufacturer');
    }

    public static function retrieveByPK($id, $isActive = null) {
        $q = self::getInstance()->createQuery()
                ->where("id = ?", $id);

        if ($isActive) {
            $q->addWhere('is_active = ?', $isActive);
        }

        return $q->fetchOne();
    }

    public static function getList($isActive = null) {
        $query = self::getInstance()->createQuery();
        if ($isActive) {
            $query->where('is_active = ?', $isActive);
        }

        return $query->execute();
    }

    public static function getForSelect($isAll = false) {
        $rows = self::getInstance()->createQuery()
                ->where("is_active = ?", self::ACTIVE)
                ->orderBy('name ASC')
                ->fetchArray();

        $arr = array();

        if ($isAll) {
            return $rows;
        }

        foreach ($rows as $row) {
            $arr[$row['id']] = $row['name'];
        }

        return $arr;
    }

    /**
     * Нэр авах
     * 
     * @param int $manifactureId
     * @return string
     */
    public static function getNameById($manifactureId) {
        $query = self::getInstance()->createQuery()
                ->where('id = ?', $manifactureId)
                ->orderBy('name ASC');

        $rows = $query->fetchArray();

        if (count($rows)) {
            return $rows[0]['name'];
        }

        return '';
    }

}
